package com.seanLab.service;

import com.seanLab.domain.Article;
import com.seanLab.domain.Image;
import com.seanLab.domain.TagInfo;
import com.seanLab.dto.SuggestArticleKeywordsDto;
import com.seanLab.dto.SuggestModelArticleDto;
import com.seanLab.repository.ArticleRepo;
import com.seanLab.repository.ImageRepo;
import com.seanLab.tool.DataExtractor;
import com.seanLab.tool.TagSuggestModel.StaticModelProperties;
import com.seanLab.tool.TagSuggestor;
import com.seanLab.tool.es.ESImageSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;

/**
 * Created by ailly on 17-10-17.
 */
public class RunImageTagging implements Runnable {

    private ArticleRepo articleRepo;

    private ImageRepo imageRepo;

    private TagSuggestor ts;

    private BlockingDeque<Article> tmpArticleQueue;

    public RunImageTagging(BlockingDeque<Article> queue, ArticleRepo articleRepo, ImageRepo imageRepo) {
        this.tmpArticleQueue = queue;
        this.ts = new TagSuggestor();
        this.ts.loadModel(StaticModelProperties.ModelPath);
        this.articleRepo = articleRepo;
        this.imageRepo = imageRepo;
    }

    @Override
    public void run() {
        while (!tmpArticleQueue.isEmpty()) {
            Article tmpExistArticle = tmpArticleQueue.poll();

            List<Image> tmpImageList = new ArrayList<>();
            for (String tmpImgID : tmpExistArticle.getImgID()) {
                Image tmpImage = this.imageRepo.findByImgID(tmpImgID);
                tmpImageList.add(tmpImage);
            }
            //抽取图片标签
            List<Image> tmpImageListWithTags = this.extractImgTagsFromArticle(tmpExistArticle, tmpImageList);
            //更新图片的标签
            this.imageRepo.save(tmpImageListWithTags);

            //更新文章关键词
            Article tmpArticleWithKeywords = this.extractKeywordsFromArticle(tmpExistArticle);
            tmpArticleWithKeywords.setExtractImgFlag(1);
            this.articleRepo.save(tmpArticleWithKeywords);

            Thread.yield();
        }
    }

    private Article extractKeywordsFromArticle(Article article) {
        Article tmpArticle = article;
        //@吴小波 TagSuggestor应返回该文章对应的关键词. e.g., articleTagList
        //@吴小波 填充以下代码
        //TagSuggestor t1 = new TagSuggestor();
        //SuggestModelArticleDto suggestModelArticleDto = DataExtractor.extractModelArticleFromArticle(article);
        //List<String> keywords = t1.doKeyword(suggestModelArticleDto);

        //fake data
        //List<String> keywords = new ArrayList<String>(Arrays.asList("keyword1", "keyword2"));

        //FIXME 吴晓波代码实现部分（not reviewed）
        TagSuggestor ts = new TagSuggestor();
        ts.loadModel(StaticModelProperties.ModelPath);
        SuggestModelArticleDto suggestModelArticleDto = DataExtractor.extractModelArticleWithoutImgsFromArticle(article);
        List<SuggestArticleKeywordsDto> keywords = ts.doKeyword(suggestModelArticleDto);

        tmpArticle.setKeywords(keywords);
        return tmpArticle;
    }

    private List<Image> extractImgTagsFromArticle(Article article, List<Image> imageList) {
        List<Image> tmpImgList = new ArrayList<Image>(imageList);
        //抽取图片关键词

        //ArrayList<ArrayList<TagInfo>> articleTagList = new ArrayList<ArrayList<TagInfo>>();
        //List<TagInfo> imgTagList = new ArrayList<>();

        // @吴小波 TagSuggestor应返回该文章对应的图片的标签列表. e.g., articleTagList
        // @吴小波 填充以下代码
        //TagSuggestor t1 = new TagSuggestor();
        //SuggestModelArticleDto suggestModelArticleDto = DataExtractor.extractModelArticleFromArticle(article);
        //ArrayList<ArrayList<TagInfo>> articleTagList = t1.doTag(suggestModelArticleDto);

        //FIXME 吴晓波代码实现部分（not reviewed）
//        TagSuggestor ts = new TagSuggestor();
//        ts.loadModel(StaticModelProperties.ModelPath);
        TagSuggestor ts = this.ts;
        SuggestModelArticleDto suggestModelArticleDto = DataExtractor.extractModelArticleFromArticle(article, imageList);

        long start = System.currentTimeMillis();

        List<List<TagInfo>> articleTagList = ts.doTag(suggestModelArticleDto);

        long end = System.currentTimeMillis();
        long cost = end - start;

        for (Image tmpImg : tmpImgList) {
            //FIXME 吴晓波代码实现部分（not reviewed）
            int index = tmpImgList.indexOf(tmpImg);
            tmpImg.setImgTagList(articleTagList.get(index));

        }
        return tmpImgList;
    }

}
